package com.yanqu.road.dao.impl.zhaoshang;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.zhaoshang.ZsProjectAdditionData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class ZsProjectAdditionDataDaoImpl extends TempDataObjectDao<ZsProjectAdditionData> {

    public static String getZsProjectAdditionDataSql = "select * from `t_c_zs_project_addition` where `openTime` >= ?";
    public Map<Long, ZsProjectAdditionData> getZsProjectAdditionData(int beginTime) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, ZsProjectAdditionData> map = new HashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getZsProjectAdditionDataSql);
            int i = 1;
            call.setInt(i++, beginTime);
            rs = call.executeQuery();
            while (rs.next()) {
                ZsProjectAdditionData data = parse(rs);
                map.put(data.getProjectId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateZsProjectAdditionDataSql = "update `t_c_zs_project_addition` set curioAddition=?,openTime=? where projectId=?";
    @Override
    protected boolean updateData(ZsProjectAdditionData zsProjectAdditionData) {
        zsProjectAdditionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, zsProjectAdditionData.getCurioAddition()));
        para.put(i++, new DbParameter(Types.INTEGER, zsProjectAdditionData.getOpenTime()));
        para.put(i++, new DbParameter(Types.BIGINT, zsProjectAdditionData.getProjectId()));
        if (getDb().executeNoneQuery(updateZsProjectAdditionDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertZsProjectAdditionDataSql = "insert into `t_c_zs_project_addition`(projectId,curioAddition,openTime) values (?,?,?)";
    @Override
    protected boolean addData(ZsProjectAdditionData zsProjectAdditionData) {
        zsProjectAdditionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, zsProjectAdditionData.getProjectId()));
        para.put(i++, new DbParameter(Types.INTEGER, zsProjectAdditionData.getCurioAddition()));
        para.put(i++, new DbParameter(Types.INTEGER, zsProjectAdditionData.getOpenTime()));
        if (getDb().executeNoneQuery(insertZsProjectAdditionDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected ZsProjectAdditionData parseData(ResultSet rs) throws SQLException {
        ZsProjectAdditionData zsProjectAdditionData = new ZsProjectAdditionData();
        zsProjectAdditionData.setProjectId(rs.getLong("projectId"));
        zsProjectAdditionData.setCurioAddition(rs.getInt("curioAddition"));
        zsProjectAdditionData.setOpenTime(rs.getInt("openTime"));
        return zsProjectAdditionData;
    }
}
